package cn.tedu.lucky.draw.server;

import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.repository.ActivityRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@SpringBootTest
public class ActivityJpaTets {

    @Autowired
    private ActivityRepository activityRepository;

    @Test
    public void selectAll(){
        List<Activity> activityList = activityRepository.findAll();
        activityList.forEach(activity -> System.out.println(activity));
    }

    @Test
    public void selectAllSort(){

        Sort sort = Sort.by(
                Sort.Order.asc("sort"),
                Sort.Order.desc("id")
        );

        List<Activity> activityList = activityRepository.findAll(sort);
        activityList.forEach(activity -> System.out.println(activity));
    }

    @Test
    public void selectActivityById(){
        /**
         * Optional 是一个在Java 8中引入的一个容器类（java.util.Optional）
         * 它提供了一种更优雅的方式来处理可能为null的数据
         * isPresent()：如果当前Optional包含结果，返回true，返回false
         * orElse(T other):如果有值则返回，否在返回other指定的值
         * get()：如果有值则返回，如果没有值就出现异常
         */
        Optional<Activity> optionalActivity = activityRepository.findById(1L);

        boolean present = optionalActivity.isPresent();
        Activity activity=null;

        if (present) {
            activity = optionalActivity.get();
        }

        System.out.println(present);

        System.out.println("activity = " + activity);
    }


    @Test
    public void saveActivity(){
        Activity activity = new Activity(null,"活动名称", "活动描述", "123", LocalDateTime.now(),
                LocalDateTime.now(), 0, LocalDateTime.now(),LocalDateTime.now(), 99);
        Activity activity1 = activityRepository.save(activity);
        System.out.println("activity1 = " + activity1);
    }

    @Test
    public void saveAll(){
        //创建活动列表，用于批量录入
        Activity activity1 = new Activity(null,"活动名称", "活动描述", "123", LocalDateTime.now(),
                LocalDateTime.now(), 0, LocalDateTime.now(),LocalDateTime.now(), 99);
        Activity activity2 = new Activity(null,"活动名称", "活动描述", "123", LocalDateTime.now(),
                LocalDateTime.now(), 0, LocalDateTime.now(),LocalDateTime.now(), 99);
        Activity activity3 = new Activity(null,"活动名称", "活动描述", "123", LocalDateTime.now(),
                LocalDateTime.now(), 0, LocalDateTime.now(),LocalDateTime.now(), 99);

        List<Activity> activities = Arrays.asList(activity1, activity2, activity3);
        List<Activity> activityList = activityRepository.saveAll(activities);
        activityList.forEach(activity -> System.out.println(activity));
    }

    @Test
    public void deleteById(){
        activityRepository.deleteById(195L);
    }

    @Test
    public void deleteByActivity(){
        Optional<Activity> optional = activityRepository.findById(194L);
        Activity activity = optional.get();
        activityRepository.delete(activity);
    }

    @Test
    public void updateById(){
        Optional<Activity> optional = activityRepository.findById(193L);
        Activity activity = optional.get();
        System.out.println(activity);
        activity.setDescription("俺是老李俺最帅，俺是一个小白菜~");
        Activity updateActivity = activityRepository.save(activity);
        System.out.println(updateActivity);
    }

    /**
     * 根据活动名称和活动状态 结合Sort值进行升序排序查询
     */


    @Test
    public void findByNameAndStatusOrderBySortAsc(){
        String name="活动名称";

        Integer status=0;
        List<Activity> activities = activityRepository.findByNameAndStatusOrderBySortDesc(name, status);

        activities.forEach(activity -> System.out.println(activity));
    }

    /**
     * 查询ID在1-5之间的活动数据
     */

    @Test
    public void findActivitiesByIdBetween(){
        List<Activity> activities = activityRepository.findActivitiesByIdBetween(180L, 185L);
        activities.forEach(activity -> System.out.println(activity));
    }

    @Test
    public void findActivityByName(){
        Activity activity = activityRepository.findActivityByNameEquals("年会欢聚堂");
        System.out.println(activity);
    }
}
